import java.util.ArrayList;
import java.util.List;

public class Test2 {
    public static List<Integer> findAnagrams(String s, String p) {
        List<Integer> list =  new ArrayList<>();
        int[] hash1 = new int[26];
        int count  =0;
        int[] hash2 = new int[26];
        for(int i =0 ;i < p.length() ; i++){
            char ch = p.charAt(i);
            hash1[ch-97] ++;
        }
        for(int left =0 , right =0 ; right < s.length() ; right++){
            char in = s.charAt(right);
            hash2[in-97]++;
            if(hash2[in-97] <= hash1[in-97]){
                count++;
            }
            if(right - left + 1 > p.length()){
                char out = s.charAt(left);
                if(hash2[out-97] <= hash1[out-97]){
                    count--;
                }

                hash2[out -97]--;
                left++;

            }
            if(count == p.length()){
                list.add(left);
            }

        }
        return list;

}

    public static void main(String[] args) {
        findAnagrams("cbaebabacd","abc");
    }
}
